package com.ticket.system.service.impl;

import com.github.pagehelper.PageInfo;
import com.ticket.common.constant.UserConstants;
import com.ticket.common.utils.NumberReplaceUtils;
import com.ticket.system.domain.YanchuOrderDetail;
import com.ticket.system.domain.vo.TicketFolderDetailVo;
import com.ticket.system.domain.vo.TicketFolderVo;
import com.ticket.system.mapper.YanchuTicketFolderMapper;
import com.ticket.system.service.IYanchuTicketFolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 小程序——票夹业务类
 *
 * @date 2023-10-18
 */
@Service
public class YanchuTicketFolderServiceImpl implements IYanchuTicketFolderService {
    @Autowired
    private YanchuTicketFolderMapper yanchuTicketFolderMapper;

    /**
     * (小程序) 分页——根据当前登录用户查询票夹信息
     *
     * @param userId 当前登录用户id
     * @return
     */
    @Override
    public PageInfo<TicketFolderVo> applyPageTicketFolder(Long userId,Long userinfoId,String phone) {
        List<TicketFolderVo> list = yanchuTicketFolderMapper.pageGetTicketFolder(userId,userinfoId,phone);
        PageInfo<TicketFolderVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * (小程序) 分页——根据当前登录用户查询历史票根
     *
     * @param userId 当前登录用户id
     * @return
     */
    @Override
    public PageInfo<TicketFolderVo> applyPageHistoryCounterfoil(Long userId,Long userinfoId,String phone) {
        //查询演出场次已取消或已观看的票据信息
        List<TicketFolderVo> list = yanchuTicketFolderMapper.pageHistoryCounterfoil(userId,userinfoId,phone);
        PageInfo<TicketFolderVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * (小程序) 票夹详情
     *
     * @param orderId 订单id
     * @param userId  当前登录用户id
     * @return
     */
    @Override
    public TicketFolderVo applyGetTicketFolderDetail(Long orderId, Long userId) {
        TicketFolderVo ticketFolderVo = yanchuTicketFolderMapper.selectPerformInfoByOrderId(orderId);
        if (null == ticketFolderVo) {
            return null;
        }
        //根据条件查询详情；条件：
        // 1、下单人id与当前用户id相等，查询订单所有票据(订单详情)信息；
        // 2、下单人id与当前用户id不相等，仅查询与当前用户关联的票据（详情）
        Long curLoginUserId = ticketFolderVo.getOrderUserId().equals(userId) ? null : userId;
        YanchuOrderDetail yanchuOrderDetail = new YanchuOrderDetail();
        yanchuOrderDetail.setOrderId(orderId);
        if (null != curLoginUserId) {
            yanchuOrderDetail.setUserinfoId(curLoginUserId);
        }
        List<TicketFolderDetailVo> ticketFolderDetailVos = yanchuTicketFolderMapper.selectTicketFolderDetail(yanchuOrderDetail);
        if (!CollectionUtils.isEmpty(ticketFolderDetailVos)) {
            //当演出场次需要实名认证时，遍历票夹详情查询用户信息，并根据证件类型判断处理证件号
            for (TicketFolderDetailVo ticketFolderDetailVo : ticketFolderDetailVos) {
                if (ticketFolderDetailVo.getIdType() == UserConstants.ID_TYPE_IDENTITY_CARD) {
                    //身份证
                    ticketFolderDetailVo.setEncryptIdNumber(NumberReplaceUtils.idCardAddStar(ticketFolderDetailVo.getIdNumber()));
                } else {
                    //护照
                    ticketFolderDetailVo.setEncryptIdNumber(NumberReplaceUtils.passportAddStar(ticketFolderDetailVo.getIdNumber()));
                }
                ticketFolderDetailVo.setIdNumber(null);
            }
        }
        ticketFolderVo.setTicketFolderDetailList(ticketFolderDetailVos);
        return ticketFolderVo;
    }
}
